package com.bizmerce.rnd.oms.criterion.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.bizmerce.rnd.oms.criterion.vo.AgreeVO;
import com.bizmerce.rnd.oms.criterion.vo.ConfirmDocIssueVO;
import com.bizmerce.rnd.oms.criterion.vo.SendbackVO;
import com.bizmerce.rnd.oms.criterion.vo.SupplOrigInfoVO;
import com.bizmerce.rnd.oms.criterion.vo.SupplPartNoVO;
import com.bizmerce.rnd.oms.util.ParamMap;

import egovframework.rte.psl.dataaccess.EgovAbstractDAO;

@Repository("criterionDAO")
@SuppressWarnings("unchecked")
public class CriterionDAO extends EgovAbstractDAO {

    /**
     * 미결 부품을 조회한다.
     */
    public List <SupplPartNoVO> selectUnsettledParts(ParamMap param) {
 
        Map <String, String> hm = new HashMap <String, String>();
        hm.put("CMPY_CD", param.getString("cmpyCd"));
        hm.put("STAT", param.getString("stat"));
        hm.put("PART_NO", param.getString("searchPartNo")); // 검색조건
        hm.put("START", param.getString("start")); // 페이징
        hm.put("END", param.getString("end")); // 페이징

        return list("Criterion.selectSupplPartNo", hm);
    }

    /**
     * 미결 부품의 변경 HSCODE와 반송사유를 저장한다.
     */
    public int saveUnsettledParts(String userId, String cmpyCd, String partNo, String supplHscd, String retnrea) {
        Map <String, String> hm = new HashMap <String, String>();
        hm.put("AMEND_ID", userId);
        hm.put("CMPY_CD", cmpyCd);
        hm.put("PART_NO", partNo); // 검색조건
        hm.put("SUPPL_HSCD", supplHscd);
        hm.put("RETNREA", retnrea);

        return update("Criterion.updateSupplPartNo", hm);
    }

    /**
     * 미결 부품을 반송시킨다
     * 
     * @param stat 상태코드 (A-미결, B-반송요청, C-반송승인, D-확인서발급)
     */
    public int sendbackUnsettledParts(String userId, String cmpyCd, String partNo, String retnrea, String stat) {
        Map <String, String> hm = new HashMap <String, String>();
        hm.put("AMEND_ID", userId);
        hm.put("CMPY_CD", cmpyCd);
        hm.put("PART_NO", partNo);
        hm.put("STAT", stat);
        hm.put("RETNREA", retnrea);

        return update("Criterion.updateSupplPartNo", hm);
    }

    /**
     * 확인서작성이 가능한 부품들을 조회한다.
     * 
     * @param param
     * @return
     */
    public List <SupplPartNoVO> selectConfirmParts(ParamMap param) {

        Map <String, String> hm = new HashMap <String, String>();
        hm.put("CMPY_CD", param.getString("cmpyCd"));
        hm.put("PART_NO", param.getString("searchPartNo"));
        hm.put("STAT", param.getString("searchStat"));
        hm.put("START", param.getString("start")); // 페이징
        hm.put("END", param.getString("end")); // 페이징

        return list("Criterion.selectSupplPartNo", hm);
    }

    /**
     * 확인서작성을 위한 부품들을 협정별로 조회한다. (확인서작성)
     * 
     * @param param
     */
    public List <SupplPartNoVO> selectConfirmPartsFTA(ParamMap param) {

        Map <String, Object> hm = new HashMap <String, Object>();
        hm.put("CMPY_CD",      param.getString("cmpyCd"));
        hm.put("PART_NO_LIST", param.getList("checkIdx"));
        hm.put("STAT",         param.getString("searchStat"));

        return list("Criterion.selectSupplPartNo", hm);
    }

    /**
     * 확인서작성을 위한 부품들을 조회한다. (확인서작성All)
     * 
     * @param param
     */
    public List <SupplPartNoVO> selectConfirmPartsFTAAll(ParamMap param) {

        Map <String, String> hm = new HashMap <String, String>();
        hm.put("CMPY_CD", param.getString("cmpyCd"));
        hm.put("PART_NO", param.getString("searchPartNo"));
        hm.put("STAT",    param.getString("searchStat"));

        return list("Criterion.selectSupplPartNo", hm);
    }

    /**
     * 확인서 발급번호를 생성한다.
     * 
     * @param param
     */
    public String selectNewCoIssno(String cmpyCd) {
        Map <String, String> hm = new HashMap <String, String>();
        hm.put("CMPY_CD", cmpyCd);

        return (String) selectByPk("Criterion.selectNewCoIssno", hm);
    }

    /**
     * 업체에서 확인서를 제출한다.
     */
    public void insertConfirmPartsFTA1(String cmpyCd, String partNo, String coIssno, String agreeCd, String rule, String vaRate, String origSatiYn, String orig, String orgCondition) {

        Map <String, String> hm = new HashMap <String, String>();
        hm.put("CMPY_CD", cmpyCd);
        hm.put("PART_NO", partNo);
        hm.put("CO_ISSNO", coIssno);
        hm.put("AGREE_CD", agreeCd);
        hm.put("RULE", rule);
        hm.put("VA_RATE", vaRate);
        hm.put("ORIG_SATI_YN", origSatiYn);
        hm.put("ORIG", orig);
        hm.put("ORG_CONDITION", orgCondition);

        insert("Criterion.insertConfirmPartsFTA1", hm);
    }

    /**
     * 업체에서 확인서 제출시 공급자정보를 공급자원산지정보로 복사함.
     */
    public int insertConfirmPartsFTA2(String userId, String userDegree, String cmpyCd, String coIssno) {

        Map <String, String> hm = new HashMap <String, String>();
        hm.put("USER_ID", userId);
        hm.put("USER_DEGREE", userDegree);
        hm.put("CMPY_CD", cmpyCd);
        hm.put("CO_ISSNO", coIssno);

        return update("Criterion.insertConfirmPartsFTA2", hm);
    }

    /**
     * 부품 상태코드 변경
     */
    public int updateSupplPartNo(String userId, String cmpyCd, String partNo, String stat) {
        Map <String, String> hm = new HashMap <String, String>();
        hm.put("AMEND_ID", userId);
        hm.put("CMPY_CD", cmpyCd);
        hm.put("PART_NO", partNo);
        hm.put("STAT", stat);
        return update("Criterion.updateSupplPartNo", hm);
    }

    /**
     * 원산지확인서발급관리-품목으로 조회.
     */
    public List <SupplOrigInfoVO> selectConfirmDocIssue1(ParamMap param) {
        Map <String, String> hm = new HashMap <String, String>();
        hm.put("CMPY_CD", param.getString("cmpyCd"));
        hm.put("PART_NO", param.getString("searchPartNo")); // 검색조건
        hm.put("START",   param.getString("start")); // 페이징
        hm.put("END",     param.getString("end")); // 페이징

        return list("Criterion.selectConfirmDocIssue1", hm);
    }

    /**
     * 원산지확인서발급관리-확인서번호로 조회.
     */
    public List <ConfirmDocIssueVO> selectConfirmDocIssue2(ParamMap param) {
        Map <String, String> hm = new HashMap <String, String>();
        hm.put("CMPY_CD",  param.getString("cmpyCd"));
        hm.put("CO_ISSNO", param.getString("searchCoIssono")); // 검색조건
        hm.put("START",    param.getString("start")); // 페이징
        hm.put("END",      param.getString("end")); // 페이징

        return list("Criterion.selectConfirmDocIssue2", hm);
    }

    /**
     * 반송관리-조회
     */
    public List <SendbackVO> selectSendback(ParamMap param) {
        Map <String, String> hm = new HashMap <String, String>();
        hm.put("STAT",     param.getString("searchStat"));   // 검색조건
        hm.put("CMPY_CD",  param.getString("searchCmpyCd")); // 검색조건
        hm.put("START",    param.getString("start"));        // 페이징
        hm.put("END",      param.getString("end"));          // 페이징
        
        return list("Criterion.selectSendback", hm);
    }

    /**
     * 반송관리-재요청
     */
    public void requestSendBack(String partNo) {

    }

    /**
     * 적용 협정 조회
     */
    public List <AgreeVO> selectApplyAgree() {
        return list("Criterion.selectApplyAgree", null);
    }

}
